package tp.pr5;

import tp.pr5.instructions.*;
import tp.pr5.instructions.exceptions.WrongInstructionFormatException;



/**
 * The interpreter is in charge of converting the user input in an instruction for the robot. Up to now, the valid instructions are:
 *
 *   MOVE
 *   TURN { LEFT | RIGHT }
 *   HELP
 *   QUIT
 * @author Carlos Bermúdez y Mario de Benito
 *
 */
public class Interpreter {
	private static Instruction[] prototypes= {
		new MoveInstruction(), 
		new TurnInstruction(),
		new PickInstruction(),
		new ScanInstruction(),
		new OperateInstruction(),
		new RadarInstruction(),
		new DropInstruction(),
		new HelpInstruction(),
		new QuitInstruction(),
		new UndoInstruction()
		
	};
	

	
	
	/**
 * It returns information about all the instructions that the robot understands
 * The interpreter is in charge of converting the user input in an instruction for the robot. Up to now, the valid instructions are:

 * @return A string with the information about all the available instructions
 */
	
	public static String interpreterHelp(){
		String ret = "The valid instructions for WALL-E are:"+Main.LINE_SEPARATOR;
				
		/*		+"     MOVE"+Main.LINE_SEPARATOR
				+"     TURN <LEFT | RIGHT>"+Main.LINE_SEPARATOR
				+"     PICK <ITEM>"+Main.LINE_SEPARATOR
				+"     SCAN [ <ITEM> ]"+Main.LINE_SEPARATOR
				+"     OPERATE <ITEM>"+Main.LINE_SEPARATOR
				+"     RADAR"+Main.LINE_SEPARATOR	
				+"     DROP <ITEM>"+Main.LINE_SEPARATOR	
				+"     HELP"+Main.LINE_SEPARATOR
				+"     QUIT"+Main.LINE_SEPARATOR;		
		*/
		for (int i=0;i<prototypes.length;i++){
			try{	
				ret+= prototypes[i].getHelp()+Main.LINE_SEPARATOR;
			}catch(Exception e){
				
			}
		}	
		return ret;
		
	}
	/**
	 * Parses the line input into a valid instruction
	 * @param line
	 * @return
	 * @throws WrongInstructionFormatException
	 */
	public static Instruction parse(String line) throws WrongInstructionFormatException{
		Instruction ins = null;
		
		for (int i=0;i<prototypes.length && ins == null;i++){
			try{	
				ins = prototypes[i].parse(line);
			}catch(Exception e){
				
			}
		}	
		if (ins == null){
			throw new WrongInstructionFormatException("WALL·E says: Sorry, i can not understand.");
		}else{
			return ins;
		}
		
	}
	
	/**
	 * Generates a new instruction according to the user input
	 * @param line- A string with the user input
	 * @return The instruction read from the given line. If the instruction is not correct, then it returns a not valid instruction
	 */
	public static Instruction generateInstruction(String line) throws WrongInstructionFormatException{
		
		Instruction ins = parse(line);
		return ins;	
			
	}	
	
}